﻿Imports System
Imports System.CodeDom
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.ComponentModel.Compiler
    Public Class ActivityCodeGenerator
        ' Methods
        Public Overridable Sub GenerateCode(ByVal manager As CodeGenerationManager, ByVal obj As Object)
            If (manager Is Nothing) Then
                Throw New ArgumentNullException("manager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim context As Activity = [TryCast](obj,Activity)
            If (context Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(Activity).FullName }), "obj")
            End If
            manager.Context.Push(context)
            Dim walker As New Walker
            AddHandler walker.FoundProperty, Function (ByVal w As Walker, ByVal args As WalkerEventArgs) 
                Dim currentValue As ActivityBind = [TryCast](args.CurrentValue,ActivityBind)
                If (Not currentValue Is Nothing) Then
                    If (Not args.CurrentProperty Is Nothing) Then
                        manager.Context.Push(args.CurrentProperty)
                    End If
                    manager.Context.Push(args.CurrentPropertyOwner)
                    Dim generator As ActivityCodeGenerator
                    For Each generator In manager.GetCodeGenerators(currentValue.GetType)
                        generator.GenerateCode(manager, args.CurrentValue)
                    Next
                    manager.Context.Pop
                    If (Not args.CurrentProperty Is Nothing) Then
                        manager.Context.Pop
                    End If
                End If
            End Function
            walker.WalkProperties(context, obj)
            manager.Context.Pop
        End Sub

        Protected Function GetCodeTypeDeclaration(ByVal manager As CodeGenerationManager, ByVal fullClassName As String) As CodeTypeDeclaration
            Dim str As String
            Dim str2 As String
            If (manager Is Nothing) Then
                Throw New ArgumentNullException("manager")
            End If
            If (fullClassName Is Nothing) Then
                Throw New ArgumentNullException("fullClassName")
            End If
            Helpers.GetNamespaceAndClassName(fullClassName, str, str2)
            Dim namespaces As CodeNamespaceCollection = [TryCast](manager.Context.Item(GetType(CodeNamespaceCollection)),CodeNamespaceCollection)
            If (namespaces Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", New Object() { GetType(CodeNamespaceCollection).Name }))
            End If
            Dim codeNamespace As CodeNamespace = Nothing
            Return Helpers.GetCodeNamespaceAndClass(namespaces, str, str2, codeNamespace)
        End Function

    End Class
End Namespace

